﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NF.Core.Fuzzy
{
    public class TriangularFuzzySet : IFuzzySet
    {
        public TriangularFuzzySet(double a, double c) : this(a, (a + c) / 2.0f, c)
        {
        }

        public TriangularFuzzySet(double a, double b, double c)
        {
            if (a == double.NegativeInfinity)
                a = double.MinValue;
            if (c == double.PositiveInfinity)
                c = double.MaxValue;

            if ((a > b) || (b > c))
                throw new ArgumentException();

            mA = a;
            mB = b;
            mC = c;
        }

        public double DetermineMembership(double x)
        {
            if(mA <= x && x <= mB)
                return (x - mA) / (mB - mA);
            if(mB <= x && x <= mC)
                return (mC - x) / (mC - mB);
            return 0.0;
        }

        public String Label
        {
            get { return mLabel; }
            set { mLabel = value; }
        }

        public double Maximum
        {
            get { return mB; }
        }

        public override int GetHashCode()
        {
            return mA.GetHashCode() * mB.GetHashCode() * mC.GetHashCode();
        }

        private String mLabel;
        private double mA, mB, mC;
    }
}
